package com.liangzi.service_core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liangzi.liangzi_common.exception.Assert;
import com.liangzi.liangzi_common.result.R;
import com.liangzi.liangzi_common.result.ResponseEnum;
import com.liangzi.liangzi_common.util.ArgumentEnum;
import com.liangzi.service_core.config.BossAutoConfiguration;
import com.liangzi.service_core.mapper.VisitBookingMapper;
import com.liangzi.service_core.pojo.bo.SnowflakeManager;
import com.liangzi.service_core.pojo.entity.*;
import com.liangzi.service_core.pojo.vo.VisitBookingQueryVO;
import com.liangzi.service_core.pojo.vo.VisitBookingVO;
import com.liangzi.service_core.pojo.vo.VisitPatienterVO;
import com.liangzi.service_core.service.*;
import com.liangzi.servicebase.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.basic.BasicIconFactory;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 患者预约表 前端控制器
 * </p>
 *
 * @author zhj
 * @since 2021-12-13
 */

@Api(description = "预约接口")
@RestController
@RequestMapping("/web/visitBooking")
@Slf4j
public class VisitBookingController {



    @Autowired
    private VisitBookingService visitBookingService;

    @Autowired
    private VisitBookingHistoryService visitBookingHistoryService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private VisitMedicalCardService visitMedicalCardService;

    @Autowired
    private InnerDoctorWorkService innerDoctorWorkService;

    @Autowired
    private SnowflakeManager snowflakeManager;


    @Autowired
    private InnerDoctorService innerDoctorService;




    // 添加一条预约记录
    @ApiOperation("添加一条预约记录")
    @PutMapping("addBooking/{rdSession}/{innerDoctorWorkId}")
    public R insertPatienter(
            @PathVariable String rdSession,
            @PathVariable String innerDoctorWorkId,
            @RequestBody
            @Valid VisitBookingVO visitBookingVO

            ){
        // 判断是否登录
        String userId = (String) redisUtil.get("session:" + rdSession);
        if (userId == null) {
            return  R.error().message("请重新登录");
        }
        String saveCard = innerDoctorWorkService.insertBooking( userId,innerDoctorWorkId, visitBookingVO);
        return saveCard != null ? R.ok().message("预约成功").data("bookingId",saveCard) : R.error().message("预约失败");


    }

    // 根据visitBookingId获取预约记录
    @ApiOperation("根据visitBookingId获取预约记录详情")
    @GetMapping("getBooking/{rdSession}/{visitBookingId}")
    public R getBooking(
            @PathVariable String rdSession,
            @PathVariable String visitBookingId

    ) {
        // 判断是否登录
        String userId = (String) redisUtil.get("session:" + rdSession);
        if (userId == null) {
            return R.error().message("请重新登录");
        }

        VisitBookingQueryVO  visitBookingQueryVO = visitBookingService.getBooking(userId,visitBookingId);
        return visitBookingQueryVO != null?R.ok().data("record", visitBookingQueryVO):R.error().message("查询数据错误");

    }


    // 获取所有预约记录
    @ApiOperation("获取所有预约记录")
    @GetMapping("getBookings/{rdSession}")
    public R getBookings(
            @PathVariable String rdSession
    ) {
        // 判断是否登录
        String userId = (String) redisUtil.get("session:" + rdSession);
        if (userId == null) {
            return R.error().message("请重新登录");
        }

        ArrayList<VisitBookingQueryVO> visitBookingQueryVOS =  visitBookingService.getBookingList(userId);
        return R.ok().data("records",visitBookingQueryVOS);

    }

    // 取消预约
    @ApiOperation("获取当前卡下的所有预约列表")
    @PostMapping("CancelBooking/{rdSession}/{bookingId}")
    public R cancelBooking(
            @PathVariable("rdSession") String rdSession,
            @PathVariable("bookingId") String bookingId

    ) {
        // 判断是否登录
        String userId = (String) redisUtil.get("session:" + rdSession);
        if (userId == null) {
            return R.error().message("请重新登录");
        }
        if("".equals(bookingId )){
            R.error().message("bookingID never not null");
        }
        boolean isCancel =  visitBookingService.cancelBooking(userId,bookingId);
        return isCancel ?R.ok().message("取消成功"):R.error().message("取消失败");
    }
}

